package org.openkanban.client.ui.adminpanel;

import org.openkanban.client.util.Md5Utils;
import org.openkanban.client.util.RootPanel;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;

/**
 * GUI Element for the SignUpForm
 */
public class SignUpForm extends DynamicForm {

    public static final String TARGET = "uploadTarget";

    private StaticTextItem message;
    private PasswordItem password;
    private PasswordItem password2;

    public SignUpForm() {
	setID("SignUpID");
	setHeight100();
	setWidth("290px");
	setPadding(3);
	setColWidths("40%", "60%");
	setStyleName("signup-form");

	// input fields
	HeaderItem header = new HeaderItem();
	header.setDefaultValue("Sign Up Form");

	// validator
	RegExpValidator emailValidator = new RegExpValidator();
	emailValidator.setErrorMessage("Invalid email address");
	emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");

	MatchesFieldValidator validator = new MatchesFieldValidator();
	validator.setOtherField("password2");
	validator.setErrorMessage("Passwords do not match");

	// input fields
	TextItem vorname = new TextItem("vorname");
	vorname.setTitle("First Name");
	vorname.setRequired(true);

	TextItem nachname = new TextItem();
	nachname.setName("nachname");
	nachname.setTitle("Last Name");
	nachname.setRequired(true);

	TextItem namensKuerzel = new TextItem();
	namensKuerzel.setName("namensKuerzel");
	namensKuerzel.setTitle("Initials");
	namensKuerzel.setRequired(true);

	// avatarUrl
	UploadItem avatarUrl = new UploadItem();
	avatarUrl.setName("avatar");
	avatarUrl.setTitle("Avatar 40x40 px");
	avatarUrl.setTitleStyle("formTitle");
	avatarUrl.setRequired(false);
	avatarUrl.setWidth(100);

	TextItem email = new TextItem();
	email.setName("email");
	email.setTitle("Email");
	email.setRequired(true);
	email.setValidators(emailValidator);

	password = new PasswordItem();
	password.setName("password");
	password.setTitle("Password");
	password.setRequired(true);
	password.setValidators(validator);

	password2 = new PasswordItem();
	password2.setName("password2");
	password2.setTitle("Password check");
	password2.setRequired(true);

	message = new StaticTextItem();
	message.setValue("signup failed!");
	message.setTitle("Error:");
	message.setTextAlign(Alignment.LEFT);
	message.setColSpan(2);
	message.setVisible(false);

	setEncoding(Encoding.MULTIPART);
	setAction("service/user/signup");
	setTarget(TARGET);

	// add components to form
	setFields(vorname, nachname, namensKuerzel, avatarUrl, email, password, password2);
    }

    @Override
    public void submit() {
	if (validate()) {
	    RootPanel.main.getAdminPanel().getVsSignUpVStack().animateHide(AnimationEffect.SLIDE, null, 250);

	    String md5Password = Md5Utils.md5(password.getValueAsString());
	    password.setValue(md5Password);
	    password2.setValue(md5Password);

	    submitForm();

	    reset();
	}
    }

    public StaticTextItem getMessage() {
	return message;
    }

}
